define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'laydate', 'bootstrap-datetimepicker'], function ($, undefined, Backend, Table, Form, laydate, datetimepicker) {

    var school_id = null;
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    first_index_url: 'school/index',
                    second_index_url: 'course/classday/index',
                    second_add_url: 'course/classday/add',
                    add_url: 'course/classday/add',
                }
            });
            this.table.first();
            this.table.second();
        },
        table: {
            first: function () {
                // 表格1
                var table1 = $("#table1");
                table1.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.first_index_url,
                    toolbar: '#toolbar1',
                    sortName: 'id',
                    search: false,
                    commonSearch: false,
                    searchFormVisible: false,
                    pagination: false,
                    columns: [
                        [
                            {field: 'id', title: __('ID'), operate: false},
                            {field: 'name', title: __('学校'), operate: false},
                            {
                                field: 'operate',
                                title: __('Operate'),
                                table: table1,
                                events: Table.api.events.operate,
                                buttons: [
                                    {
                                        name: 'log',
                                        title: '查看',
                                        text: '查看',
                                        icon: 'fa',
                                        classname: 'btn btn-success btn-xs btn-click',
                                        click: function (e, data) {
                                            $(".bg-blue").removeClass("bg-blue");
                                            $(this).addClass("bg-blue");

                                            school_id = data.id;
                                            Controller.api.select(school_id);
                                        }
                                    }
                                ],
                                formatter: Table.api.formatter.operate
                            }
                        ]
                    ]
                });

                // 为表格1绑定事件
                Table.api.bindevent(table1);

                table1.on('post-body.bs.table', function (e, json) {

                    if (school_id) {
                        $("tbody tr[data-index]", this).each(function () {
                            if (parseInt($("td:eq(0)", this).text()) == school_id) {
                                $(".btn-success", this).addClass("bg-blue");
                            }
                        });
                    } else {
                        $("tbody tr[data-index=0] .btn-success").addClass("bg-blue");
                        school_id = $("tbody tr[data-index=0] td:eq(0)").text();
                    }

                    Controller.api.select(school_id);

                });

            },
            second: function () {
                // 表格2
                var table2 = $("#table2");
                table2.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.second_index_url,
                    toolbar: '#toolbar2',
                    sortName: 'id',
                    pagination: false,
                    columns: [
                        [
                            {field: 'school_id', title: __('学校id'), visible: false},
                            {field: 'id', title: __('id'), visible: false},
                            {field: 'number', title: __('节次'), operate: false},
                            {
                                field: 'monday',
                                title: __('星期一'),
                                operate: false,
                                formatter: Controller.api.formatter.day
                            },
                            {
                                field: 'tuesday',
                                title: __('星期二'),
                                operate: false,
                                formatter: Controller.api.formatter.day
                            },
                            {
                                field: 'wednesday',
                                title: __('星期三'),
                                operate: false,
                                formatter: Controller.api.formatter.day
                            },
                            {
                                field: 'thursday',
                                title: __('星期四'),
                                operate: false,
                                formatter: Controller.api.formatter.day
                            },
                            {
                                field: 'friday',
                                title: __('星期五'),
                                operate: false,
                                formatter: Controller.api.formatter.day
                            },
                            {
                                field: 'saturday',
                                title: __('星期六'),
                                operate: false,
                                formatter: Controller.api.formatter.day
                            },
                            {
                                field: 'sunday',
                                title: __('星期日'),
                                operate: false,
                                formatter: Controller.api.formatter.day
                            },
                        ]
                    ],
                    onDblClickCell: function (field, value, row, $element) {
                        Backend.api.open('/admin/course/classday/set?id=' + value.id, __('设置'));
                    },
                    onLoadSuccess: function () {
                        $(".btn-add").removeClass("disabled");

                    }
                });

                $(document).on('click', '.btn-get-data', function () {
                    alert(JSON.stringify(table2.bootstrapTable('getData')));
                });

                // 为表格2绑定事件
                Table.api.bindevent(table2);
            }
        },
        add: function () {
            var options = {
                format: 'HH:mm',
                icons: {
                    time: 'fa fa-clock-o',
                    date: 'fa fa-calendar',
                    up: 'fa fa-chevron-up',
                    down: 'fa fa-chevron-down',
                    previous: 'fa fa-chevron-left',
                    next: 'fa fa-chevron-right',
                    today: 'fa fa-history',
                    clear: 'fa fa-trash',
                    close: 'fa fa-remove'
                },
                showTodayButton: true,
                showClose: true
            };
            $('.datetimepicker').parent().css('position', 'relative');
            $('.datetimepicker').datetimepicker(options);

            Controller.api.bindevent();
        },
        set: function () {
            var options = {
                format: 'HH:mm',
                icons: {
                    time: 'fa fa-clock-o',
                    date: 'fa fa-calendar',
                    up: 'fa fa-chevron-up',
                    down: 'fa fa-chevron-down',
                    previous: 'fa fa-chevron-left',
                    next: 'fa fa-chevron-right',
                    today: 'fa fa-history',
                    clear: 'fa fa-trash',
                    close: 'fa fa-remove'
                },
                showTodayButton: true,
                showClose: true
            };
            $('.datetimepicker').parent().css('position', 'relative');
            $('.datetimepicker').datetimepicker(options);

            $(document).on('click', '.btn-success', function () {
                var id = $('#id').val();
                $.ajax({
                    type: "POST",
                    url: "course/classday/set?id=" + id,
                    data: $("#set-form").serialize(),
                    dataType: "json",
                    success: function (data) {
                    }
                });

            })

            Form.api.bindevent($("form[role=form]"));
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            select: function (school_id) {
                $.fn.bootstrapTable.defaults.extend.add_url = $.fn.bootstrapTable.defaults.extend.second_add_url + "/id/" + school_id;
                $("#myTabContent2 .form-commonsearch input[name='school_id']").val(school_id);
                $("#myTabContent2 .btn-refresh").trigger("click");
            },
            formatter: {
                day: function (value, row, index) {
                    return value.value;
                }
            }
        }
    };
    return Controller;
});
